package com.alstudio.ui.module.main;

import java.util.ArrayList;

import android.content.Context;
import android.os.Bundle;

import com.alstudio.app.ALLocalEnv;
import com.alstudio.module.xmpp.request.ALUiXmppRequestCreator;
import com.alstudio.pojo.ALXmppEvent;
import com.alstudio.pojo.module.user.User;
import com.alstudio.ui.base.TitleBarActivity;
import com.alstudio.utils.asyncTask.ALAsycTask;
import com.alstudio.utils.asyncTask.ALAsyncTaskCallback;
import com.alstudio.view.absadapter.BaseUserListAdapter;
import com.alstudio.view.listview.PullRefreshAndLoadMoreView;
import com.alstudio.view.listview.PullRefreshAndLoadMoreView.RankViewLoadStateListener;
import com.loovee.imaohu.R;

/**
 * 基本的显示用户列表界面
 * 
 * @author alonso lee
 * 
 */
public class BasicUsersListActivity extends TitleBarActivity implements
		RankViewLoadStateListener, ALAsyncTaskCallback {

	// 相互关注
	public static final int SHOW_TYPE_FAVORITES_EACH_OTHER = 0;
	public static final int SHOW_TYPE_BLACK_LIST = 1;

	private int showType = SHOW_TYPE_FAVORITES_EACH_OTHER;
	// 显示用户数据的view
	private PullRefreshAndLoadMoreView listview;
	// 搜索结果
	private ArrayList<User> userList;
	// 数据适配器
	private BaseUserListAdapter userAdapter;

	private ALAsycTask task;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		initData();
		setupView(getApplicationContext(), getClass().getSimpleName());
		requeqtData();
	}

	@Override
	protected void setupView(Context context, String name) {
		// TODO Auto-generated method stub
		super.setupView(context, name);
		setTitleBarActivityContentView(R.layout.basic_users_list_layout);
		if (showType == SHOW_TYPE_FAVORITES_EACH_OTHER) {
			setCenterTextViewMessage(R.string.TxtSpecialCount);
		} else if (showType == SHOW_TYPE_BLACK_LIST) {
			// 黑名单
			setCenterTextViewMessage(R.string.TxtBlackList);
		}
		listview = (PullRefreshAndLoadMoreView) findViewById(R.id.listview);
		// 禁止下拉刷新
		listview.disablePullRefresh();
		// 不要声音
		listview.setSoundEffectsEnabled(false);
		userList = new ArrayList<User>();
		userAdapter = new BaseUserListAdapter(userList);
		listview.setAdapter(userAdapter);
		listview.setLoadStateListener(this);
		if (showType == SHOW_TYPE_BLACK_LIST) {
			loadBlackList();
			userAdapter.setNickOnly(true);
		}

	}

	@Override
	protected void initData() {
		// TODO Auto-generated method stub
		showType = getIntent().getIntExtra("showType",
				SHOW_TYPE_FAVORITES_EACH_OTHER);
	}

	@Override
	public void requeqtData() {
		// TODO Auto-generated method stub
		super.requeqtData();
		if (showType == SHOW_TYPE_FAVORITES_EACH_OTHER) {
			if (ALLocalEnv.isSendXmppDataEnable()) {
				ALUiXmppRequestCreator.postALUiXmppGetUserSpecialFavorites(
						ALLocalEnv.LOAD_MORE_START_INDEX + "",
						ALLocalEnv.LOAD_MORE_END_INDEX + "");
				showLoadingProgress();
			}
		}
	}

	private void loadBlackList() {
		task = new ALAsycTask(this);
		task.execute("");
	}

	@Override
	public void onLoadMore(PullRefreshAndLoadMoreView view) {
		// TODO Auto-generated method stub
		if (ALLocalEnv.isSendXmppDataEnable()) {
			ALUiXmppRequestCreator.postALUiXmppGetUserSpecialFavorites(
					view.startIndex + "", view.endIndex + "");
		} else {
			listview.onLoadMoreCompleted();
		}
	}

	@Override
	public void onRefresh(PullRefreshAndLoadMoreView view) {
		// TODO Auto-generated method stub
		if (ALLocalEnv.isSendXmppDataEnable()) {
			ALUiXmppRequestCreator.postALUiXmppGetUserSpecialFavorites(
					view.startIndex + "", view.endIndex + "");
		} else {
			listview.onRefreshCompleted();
		}
	}

	@Override
	public void handleGetFavoritesEachOtherResult(ALXmppEvent data) {
		// TODO Auto-generated method stub
		super.handleGetFavoritesEachOtherResult(data);
		dismissLoadingProgress();
		listview.onLoadCompleted();
		if (data.getResponseCode() == ALXmppEvent.RESPONSE_OK) {
			boolean hasMore = data.getBoolean();
			listview.updateLoadMoreState(hasMore);
			@SuppressWarnings("unchecked")
			ArrayList<User> tmp = (ArrayList<User>) data.getData();
			if (tmp != null && tmp.size() > 0) {
				userList.addAll(tmp);
				userAdapter.notifyDataSetChanged();
				tmp.clear();
				tmp = null;
			} else {
				// 没数据
				listview.showNoDataInfo(R.drawable.friend_default_04,
						getString(R.string.TxtNoFavoritesEachOtherHint));

			}
		}
	}

	@Override
	public Object onTaskStart(Object obj) {
		// TODO Auto-generated method stub
		ArrayList<User> tmp = ALLocalEnv.getLocalEnv().getDatabase()
				.getBlackListUsers();
		userList.addAll(tmp);
		return null;
	}

	@Override
	public Object onTaskFinish(Object obj) {
		// TODO Auto-generated method stub
		if (userList.size() == 0) {
			// 没数据
			listview.showNoDataInfo(R.drawable.friend_default_02,
					getString(R.string.TxtBlackListEmptyHint));
		} else {
			userAdapter.notifyDataSetChanged();
		}
		return null;
	}

	@Override
	public Object onTaskCancel() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void handleBlackListOptEvent() {
		// TODO Auto-generated method stub
		super.handleBlackListOptEvent();
		if (showType == SHOW_TYPE_BLACK_LIST) {
			userList.clear();
			loadBlackList();
		}
	}

}
